/**
 * 
 */
package co.unipiloto.identify.camera;

import java.io.FileNotFoundException;
import java.text.SimpleDateFormat;
import java.util.Date;


import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

/**
 * @author David
 *
 */
public class CameraControl extends Activity implements OnClickListener{
	
	private CustomCameraView cv;
	 private SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyyMMddHHmmssSS");
	   private Uri URL_SAVE = Media.EXTERNAL_CONTENT_URI;
	   
    public boolean onCreateOptionsMenu(android.view.Menu menu) {
        MenuItem item = menu.add(0, 0, 0, "goto gallery");
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(Intent.ACTION_VIEW, URL_SAVE);
                startActivity(intent);
                return true;
            }
        });
        return true;
    }
    Camera.PictureCallback mPictureCallbackRaw = new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera c) {
            Log.e(getClass().getSimpleName(), "PICTURE CALLBACK RAW: " + data);
            cv.getCamera().startPreview();
        }
    };
    
    Camera.PictureCallback mPictureCallbackJpeg= new Camera.PictureCallback() {
        public void onPictureTaken(byte[] data, Camera c) {
            Log.e(getClass().getSimpleName(), "PICTURE CALLBACK JPEG: data.length = " + data);
        }
    };
    
    Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {
    	public void onShutter() {
    		Log.e(getClass().getSimpleName(), "SHUTTER CALLBACK");
    	}
    };
    

    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
    	ImageCaptureCallback iccb = null;
    		String filename = timeStampFormat.format(new Date());
    		ContentValues values = new ContentValues();
    		values.put(Media.TITLE, filename);
    		values.put(Media.DESCRIPTION, "Image capture by camera");
    		Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values);
    		//String filename = timeStampFormat.format(new Date());
    		try {
				iccb = new ImageCaptureCallback( getContentResolver().openOutputStream(uri));
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
//        if (keyCode == KeyEvent.KEYCODE_BACK) {
//            return super.onKeyDown(keyCode, event);
//        }
 
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            cv.getCamera().takePicture(mShutterCallback, mPictureCallbackRaw, iccb);
            return true;
        }

        return false;
    }
    
	public void onCreate(Bundle savedInstanceState) {
		   try{
		      super.onCreate(savedInstanceState);
		      
		      getWindow().setFormat(PixelFormat.TRANSLUCENT);

				requestWindowFeature(Window.FEATURE_NO_TITLE);

				getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		      
		      cv = new CustomCameraView(
		         this.getApplicationContext());
		      FrameLayout rl = new FrameLayout(
		         this.getApplicationContext());
		      setContentView(rl);
		      rl.addView(cv);
		   } catch(Exception e){}
		}

	@Override
	public void onClick(View v) {
		 String str = "Hello There";
         Toast.makeText(getBaseContext(), str, 
         		Toast.LENGTH_SHORT).show();
		
	}
	  
}
